package com.lbh.cloudgallery.service.impl;

import com.lbh.cloudgallery.entity.vo.MemberLoginVo;
import com.lbh.cloudgallery.entity.vo.MemberRegistVo;
import com.xxx.common.utils.JwtUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.Query;

import com.lbh.cloudgallery.dao.MemberDao;
import com.lbh.cloudgallery.entity.MemberEntity;
import com.lbh.cloudgallery.service.MemberService;
import org.springframework.util.StringUtils;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).or().like("username",key).or().like("name",key);
        }

        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public String login(MemberLoginVo vo) {
        //获取用户名和密码
        String username=vo.getUsername();
        String password=vo.getPassword();

        QueryWrapper<MemberEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        MemberEntity memberEntity= baseMapper.selectOne(wrapper);
        if(memberEntity!=null && password.equals(memberEntity.getPassword())){
            return JwtUtils.getJwtToken(memberEntity.getUsername(),memberEntity.getName());
        }else{
            return null;
        }
    }

    //用户注册
    @Override
    public void register(MemberRegistVo vo) {
        String username=vo.getUsername();
        String password=vo.getPassword();
        String name=vo.getName();
        String header=vo.getHeader();
        String sex=vo.getSex();
        String useremail=vo.getUserEmail();

        //判断用户名是否重复，若重复，则不添加
        QueryWrapper<MemberEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        Integer count=baseMapper.selectCount((wrapper));
        if(count==0){
            //添加数据库中
            MemberEntity memberEntity=new MemberEntity();
            memberEntity.setUsername(username);
            memberEntity.setPassword(password);
            memberEntity.setName(name);
            memberEntity.setHeader(header);
            memberEntity.setSex(sex);
            memberEntity.setUserEmail(useremail);
            baseMapper.insert(memberEntity);
        }
    }


    /**
     *登录的方法
     * @author 来日可期
     * @param vo
     */



}